export default {
	mounted(el: HTMLElement) {
		let firstTime = ''
		let lastTime = ''

		el.onmousedown = function (e) {
			const disX = e.clientX - el.offsetLeft
			const disY = e.clientY - el.offsetTop

			document.onmousemove = function (e) {
				el.setAttribute('drag-flag', 'true')
				firstTime = new Date().getTime().toString()

				let l = e.clientX - disX
				let t = e.clientY - disY

				if (t > 0 && t < document.body.clientHeight - 50) {
					el.style.top = t + 'px'
				}
				if (l > 0 && l < document.body.clientWidth - 50) {
					el.style.left = l + 'px'
				}
			}

			document.onmouseup = function () {
				lastTime = new Date().getTime().toString()
				if (parseInt(lastTime) - parseInt(firstTime) > 200) {
					el.setAttribute('drag-flag', 'false')
				}
				document.onmousemove = null
				document.onmouseup = null
			}

			return false
		}
	}
}
